var HistoryTrendChart = function (params) {
    var self = this;
    self.chartId = params.chartId;
    self.chartQuery = params.chartQuery;
    self.option = null;
    self.chart = null;
    self.points = null;
    self.colors = ["#000", "#0f0", "00f", "#f00"];
    self.lineDataArray = new Array();
    self.seriesArray = new Array();
    self.render = function () {
        self.option.series = self.seriesArray;
        self.chart.setOption(self.option);
    };
    self.begin = function () {
        try {
            self.request();
        }
        catch (e) {
            console.log(e.message);
        }
    };
    self.dataReceived = function (point, data) {
        var lineData = self.createLineData(point.deviceCode, point.pointCode, point.eigenCode, data.data);
        self.replaceLineData(point.deviceCode, point.pointCode, point.eigenCode, lineData);
        self.replaceSeriesData(point.deviceCode, point.pointCode, point.eigenCode, lineData.list);
        self.render();
    };
    self.request = function () {
        $.each(self.points, function (i, d) {
            ajaxPost(contextRoot + "history/getEigenValue",
                d,
                function (data) {
                    self.dataReceived(d, data)
                },
                function (error) {
                    console.log(error);
                }, 30000
            );
        })
    };
    self.init = function () {
        self.chart = echarts.init(document.getElementById(self.chartId));
        self.option = {
            title: {
                left: 'center',
                text: '趋势图'
            },
            tooltip: {
                trigger: 'axis',
                formatter: function (params) {
                    params = params[0];
                    var date = new Date(params.name);
                    return '时间:' + date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ',值: ' + params.value[1];
                },
                axisPointer: {
                    animation: false
                }
            },
            xAxis: {
                type: 'time',
                max: "2018-01-01",
                min: "2017-01-01",
                splitLine: {
                    show: false
                }
            },
            yAxis: {
                type: 'value',
                max: function (value) {
                    return value.max + 5;
                },
                min: function (value) {
                    return value.min - 5;
                },
                splitLine: {
                    show: false
                }
            },
            series: []
        };
        self.render();
    };
    self.reset = function (points) {
        self.points = points;
        $.each(points, function (i, d) {
            self.lineDataArray = new Array();
            self.seriesArray = new Array();
            var lineData = self.createLineData(d.pointCode, d.eigenCode);
            self.lineDataArray.push(lineData);
            var series = self.createSeries(d.name, d.pointCode, d.eigenCode, lineData, self.colors[i % self.colors.length]);
            self.seriesArray.push(series);
        });
    };
    self.getLineData = function (deviceCode, pointCode, eigenCode) {
        var lineData = null;
        $.each(self.lineDataArray, function (i, d) {
            if (d.pointCode == pointCode && d.eigenCode == eigenCode) {
                lineData = d;
                return true;
            }
        });

        return lineData;
    };
    self.replaceLineData = function (deviceCode, pointCode, eigenCode, data) {
        $.each(self.lineDataArray, function (i, d) {
            if (d.pointCode == pointCode && d.eigenCode == eigenCode) {
                self.lineDataArray.splice(i, 1, data);
                return true;
            }
        });
    };
    self.createLineData = function (deviceCode, pointCode, eigenCode, defaultData) {
        var lineData = new Object();
        lineData.list = new Array();
        if (defaultData) {
            lineData.list = defaultData;
        }
        lineData.pointCode = pointCode;
        lineData.eigenCode = eigenCode;
        return lineData;
    };
    self.getSeries = function (deviceCode, pointCode, eigenCode) {
        var series = null;
        $.each(self.seriesArray, function (i, d) {
            if (d.pointCode == pointCode && d.eigenCode == eigenCode) {
                series = d;
                return true;
            }
        });
        return series;
    };
    self.replaceSeriesData = function (deviceCode, pointCode, eigenCode, data) {
        var series = self.getSeries(pointCode, eigenCode);
        if (series) {
            series.data = data;
        }
    }
    self.createSeries = function (name, deviceCode, pointCode, eigenCode, data, color) {
        var series = {
            name: name,
            type: 'line',
            showSymbol: false,
            hoverAnimation: false,
            data: data.list,
            itemStyle: {
                normal: {
                    color: color,
                    borderColor: '#fff',
                    borderWith: 1,
                    shadowBlur: 2,
                    shadowOffsetX: 2,
                    shadowOffsetY: 5
                }
            },
            lineStyle: {
                normal: {
                    width: 2,
                    shadowColor: 'rgba(112, 155, 233, 0.5)',
                    shadowBlur: 4,
                    shadowOffsetY: 4
                }
            },
            markLine: {
                silent: true,
                symbol: false,
                animation: false,
                label: {
                    normal: {
                        show: false
                    }
                },
                data: [{
                    yAxis: -0.10
                }],
                lineStyle: {
                    normal: {
                        opacity: 0.5
                    }
                }
            }
        };
        series.pointCode = pointCode;
        series.eigenCode = eigenCode;
        return series;
    };

    self.addTreeNode = function (treeNode) {
        self.chartQuery.addTreeNode(treeNode);
    };
}